package com.module.controller;

import com.module.framework.domain.ResponseResult;
import com.module.framework.domain.User;
import com.module.framework.dto.UserPwdDTO;
import com.module.framework.service.MenuService;
import com.module.framework.service.UserService;
import com.module.framework.vo.PageVO;
import com.module.framework.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author JiHuiQin
 * @Date 2022/3/2 20:52
 * @Version 1.0
 */

@RestController
@RequestMapping("/admin/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private MenuService menuService;

    @GetMapping("/list")
    public ResponseResult userList(Integer pageNum, Integer pageSize, String name, Integer roleId) {
        PageVO pageVO = userService.getUserList(pageNum, pageSize, name, roleId);
        return ResponseResult.okResult(pageVO);
    }

    @GetMapping("/list2")
    public ResponseResult userList2() {
        PageVO pageVO = userService.getUserList2();
        return ResponseResult.okResult(pageVO);
    }

    @GetMapping("/info")
    public ResponseResult userInfo() {
        UserInfoVO userInfoVO = userService.getUserInfo();
        return ResponseResult.okResult(userInfoVO);
    }

    @PostMapping("/add")
    public ResponseResult addUser(@RequestBody User user) {
        userService.addUser(user);
        return ResponseResult.okResult();
    }

    @PostMapping("/update")
    public ResponseResult updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return ResponseResult.okResult();
    }

    @GetMapping("/delete")
    public ResponseResult deleteUser(Integer id) {
        userService.deleteUser(id);
        return ResponseResult.okResult();
    }

    @PostMapping("/updatePwd")
    public ResponseResult updatePwd(@RequestBody UserPwdDTO userPwdDTO) {
        userService.updatePwd(userPwdDTO);
        return ResponseResult.okResult();
    }


    @GetMapping("/{userId}/permission")
    public ResponseResult permission(@PathVariable Integer userId) {
        return ResponseResult.okResult(menuService.selectPermsByUserId(userId));
    }

}
